//
//  ParkDetailViewController.m
//  CarPark
//
//  Created by oceans on 2019/7/22.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ParkDetailViewController.h"
#import "CaptureDeviceCollectionViewCell.h"
#import "DetailTextCollectionViewCell.h"
#import "CarStatusCollectionViewCell.h"
#import "WatchHeaderCollectionReusableView.h"
#import "EmptyCollectionReusableView.h"
#import "ChipDetailViewController.h"

@interface ParkDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) NSDictionary *info;
@end

@implementation ParkDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:[NSString stringWithFormat:kpark_detail, __id] paras:nil showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        weakself.info = result[@"datas"];
        [weakself setupView];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)setupView {
    
    self.navigationItem.title = _info[@"name"];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight-XBotSafeSpace-40.0) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    [self.view addSubview:_collection];
    [_collection registerNib:[UINib nibWithNibName:@"CaptureDeviceCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CaptureDeviceCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"CarStatusCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CarStatusCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"DetailTextCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"DetailTextCollectionViewCell"];
    [_collection registerNib:[UINib nibWithNibName:@"WatchHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView"];
    [_collection registerClass:[EmptyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EmptyCollectionReusableView"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"电话联系" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, ScreenHeight-XBotSafeSpace-40.0, ScreenWidth, 40.0+XBotSafeSpace);
    [button addTarget:self action:@selector(call) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = COLOR(101, 101, 101, 1);
    [self.view addSubview:button];
}

- (void)call {
    
    defweakself
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"电话" message:_info[@"contact_tel"] preferredStyle:UIAlertControllerStyleAlert];
    [con addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil]];
    [con addAction:[UIAlertAction actionWithTitle:@"立即呼叫" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *tel = weakself.info[@"contact_tel"];
        tel = [@"telprompt://" stringByAppendingString:tel];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel] options:@{} completionHandler:nil];
        }else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }]];
    con.modalPresentationStyle = 0;
    [self presentViewController:con animated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 3;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(ScreenWidth, 45.0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    return CGSizeMake(ScreenWidth, 10.0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        WatchHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WatchHeaderCollectionReusableView" forIndexPath:indexPath];
        view.rightImageView.hidden = YES;
        if (indexPath.section == 0) {
            view.titleLabel.text = @"今日动态";
        }else if (indexPath.section == 1) {
            view.titleLabel.text = [NSString stringWithFormat:@"捕捉设备 (%d)", [_info[@"capture_datas"][@"captures_nums"] intValue]];
        }else {
            view.titleLabel.text = [NSString stringWithFormat:@"车辆状态 (%d)", [_info[@"autocars_datas"][@"auto_car_nums"] intValue]];
        }
        return view;
    }else {
        
        EmptyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EmptyCollectionReusableView" forIndexPath:indexPath];
        view.backgroundColor = COLOR(242, 242, 242, 1);
        return view;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 0) {
        return 50.0;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake((ScreenWidth-50.0)/2.0, 45.0);
    }
    return CGSizeMake(ScreenWidth, 40.0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 4;
    }else if (section == 1) {
        return [(NSArray *)_info[@"capture_datas"][@"captures"] count]+1;
    }else {
        return [(NSArray *)_info[@"autocars_datas"][@"exception_datas"] count]+1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        DetailTextCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailTextCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.leftLabel.text = @"今日新增车辆";
            cell.rightLabel.text = [NSString stringWithFormat:@"%@辆", _info[@"today"][@"autocar_new_num"]];
        }else if (indexPath.item == 1) {
            cell.leftLabel.text = @"今日出售车辆";
            cell.rightLabel.text = [NSString stringWithFormat:@"%@辆", _info[@"today"][@"autocar_sell_num"]];
        }else if (indexPath.item == 2) {
            cell.leftLabel.text = @"今日调入车辆";
            cell.rightLabel.text = [NSString stringWithFormat:@"%@辆", _info[@"today"][@"dispatch_in"]];
        }else {
            cell.leftLabel.text = @"今日调出车辆";
            cell.rightLabel.text = [NSString stringWithFormat:@"%@辆", _info[@"today"][@"dispatch_out"]];
        }
        return cell;
    }else if (indexPath.section == 1) {
        CaptureDeviceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CaptureDeviceCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.backgroundColor = COLOR(153, 153, 153, 1);
            cell.label1.text = [NSString stringWithFormat:@"编号"];
            cell.label2.text = [NSString stringWithFormat:@"名称"];
            cell.label3.text = [NSString stringWithFormat:@"状态"];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
            NSDictionary *dic = _info[@"capture_datas"][@"captures"][indexPath.item-1];
            cell.label1.text = [NSString stringWithFormat:@"%@", dic[@"id"]];
            cell.label2.text = [NSString stringWithFormat:@"%@", dic[@"name"]];
            cell.label3.text = [NSString stringWithFormat:@"%@", dic[@"status_text"]];
        }
        return cell;
    }else {
        CarStatusCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarStatusCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.backgroundColor = COLOR(153, 153, 153, 1);
            cell.label1.text = [NSString stringWithFormat:@"芯片号"];
            cell.label2.text = [NSString stringWithFormat:@"状态"];
            cell.label3.text = [NSString stringWithFormat:@"车辆名称"];
            cell.label4.text = [NSString stringWithFormat:@"车架号"];
            cell.label1.textColor = [UIColor blackColor];
            cell.label2.textColor = [UIColor blackColor];
            cell.label3.textColor = [UIColor blackColor];
            cell.label4.textColor = [UIColor blackColor];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
            NSDictionary *dic = _info[@"autocars_datas"][@"exception_datas"][indexPath.item-1];
            cell.label1.text = [NSString stringWithFormat:@"%@", dic[@"rfid_id"]];
            cell.label2.text = [NSString stringWithFormat:@"%@", dic[@"monitor_status_text"]];
            cell.label3.text = [NSString stringWithFormat:@"%@", dic[@"car_model"]];
            cell.label4.text = [NSString stringWithFormat:@"%@", dic[@"vin_no"]];
            if ([dic[@"monitor_status"] intValue] == 0) {
                cell.label1.textColor = [UIColor redColor];
                cell.label2.textColor = [UIColor redColor];
                cell.label3.textColor = [UIColor redColor];
                cell.label4.textColor = [UIColor redColor];
            }else if ([dic[@"monitor_status"] intValue] == 10) {
                cell.label1.textColor = [UIColor blackColor];
                cell.label2.textColor = [UIColor blackColor];
                cell.label3.textColor = [UIColor blackColor];
                cell.label4.textColor = [UIColor blackColor];
            }else if ([dic[@"monitor_status"] intValue] == 11) {
                cell.label1.textColor = COLOR(255, 253, 75, 1);
                cell.label2.textColor = COLOR(255, 253, 75, 1);
                cell.label3.textColor = COLOR(255, 253, 75, 1);
                cell.label4.textColor = COLOR(255, 253, 75, 1);
            }else if ([dic[@"monitor_status"] intValue] == 20) {
                cell.label1.textColor = [UIColor redColor];
                cell.label2.textColor = [UIColor redColor];
                cell.label3.textColor = [UIColor redColor];
                cell.label4.textColor = [UIColor redColor];
            }
        }
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2 && indexPath.item != 0) {
        
        ChipDetailViewController *con = [[ChipDetailViewController alloc] init];
        NSDictionary *dic = _info[@"autocars_datas"][@"exception_datas"][indexPath.item-1];
        con._id = [NSString stringWithFormat:@"%@", dic[@"id"]];
        [self.navigationController pushViewController:con animated:YES];
    }
}

@end
